package com.qd.panda.plate;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.plate.UserPlateApprovalDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.plate.UserPlateApprovalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * WEB_车牌审核
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/userPlateApproval/")
@Slf4j
public class UserPlateApprovalController {
    private final UserPlateApprovalService userPlateApprovalService;

    /**
     * 审核
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:userPlateApproval:approval')")
    @OperatorInfo(title = "审核", operator = OperatorType.OTHER)
    @PostMapping(value = "/approval")
    public ApiResultWrapper<Object> approval(@Validated @RequestBody UserPlateApprovalDTO dto) {
        if(!StringUtils.hasLength(dto.getApprovalId())){
            return ApiResult.error("待审核ID不能为空");
        }
        if(null == dto.getApprovalStatus()){
            return ApiResult.error("审核状态不能为空");
        }
        if(dto.getApprovalStatus().equals(PandaConstant.APPROVAL_STATUS_2)){
            if(!StringUtils.hasLength(dto.getApprovalMsg())){
                return ApiResult.error("审批意见不能为空");
            }
        }
        dto.setApprovalUser(SecurityUtils.getLoginUser().getUserId());
        dto.setApprovalUnit(SecurityUtils.getLoginUser().getCurrentOrgId());
        userPlateApprovalService.approval(dto);
        return ApiResult.success();
    }


    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserPlateApprovalDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return userPlateApprovalService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserPlateApprovalDTO> page(UserPlateApprovalDTO dto) {
        return userPlateApprovalService.page(PageUtil.buildPage(dto), dto);
    }
}